<template>
  <div class="login-container">
    <!-- 左侧区域：背景图和标题 -->
    <div class="login-left">
      <div class="login-left-content">
        <div class="login-logo">
          <h1>智慧校园</h1>
          <p>Campus Management System</p>
        </div>
        <div class="login-slogan">
          <h2>智能·高效·便捷</h2>
          <p>打造现代化校园管理平台</p>
        </div>
      </div>
      <div class="login-bg-overlay"></div>
    </div>
    
    <!-- 右侧区域：登录表单 -->
    <div class="login-right">
      <div class="login-form-container">
        <div class="login-header">
          <h2>用户登录</h2>
          <p>欢迎回来，请登录您的账号</p>
        </div>
        
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
          <!-- 用户类型选择 -->
          <el-form-item prop="userType">
            <el-radio-group v-model="loginForm.userType" size="large">
              <el-radio-button label="student">学生</el-radio-button>
              <el-radio-button label="teacher">教师</el-radio-button>
            </el-radio-group>
          </el-form-item>
          
          <!-- 用户名/学号/工号 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :placeholder="loginForm.userType === 'student' ? '请输入学号' : '请输入工号'"
              size="large"
              class="custom-input"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              size="large"
              show-password
              class="custom-input"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <!-- 验证码 (仅学生需要) -->
          <el-form-item prop="code" v-if="loginForm.userType === 'student'">
            <div class="code-input">
              <el-input
                v-model="loginForm.code"
                placeholder="请输入验证码"
                size="large"
                class="custom-input"
                @keyup.enter="handleLogin"
              >
                <template #prefix>
                  <el-icon><Key /></el-icon>
                </template>
              </el-input>
              <el-button type="primary" class="captcha-btn" @click="refreshCaptcha">获取验证码</el-button>
            </div>
          </el-form-item>
          
          <!-- 记住密码 -->
          <el-form-item>
            <div class="remember-forgot">
              <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
              <el-button text type="primary">忘记密码?</el-button>
            </div>
          </el-form-item>
          
          <!-- 登录按钮 -->
          <el-button :loading="loading" type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form>
        
        <div class="login-footer">
          <p>© {{ new Date().getFullYear() }} 智慧校园管理系统 版权所有</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Key } from '@element-plus/icons-vue'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  userType: 'student', // 默认为学生登录
  username: '',
  password: '',
  code: '',
  remember: false
})

// 表单验证规则
const loginRules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 4, message: '验证码长度为4位', trigger: 'blur' }
  ],
  userType: [
    { required: true, message: '请选择用户类型', trigger: 'change' }
  ]
})

// 监听用户类型变化，重置表单
watch(() => loginForm.userType, (newType) => {
  loginForm.username = ''
  loginForm.password = ''
  loginForm.code = ''
  
  // 重新设置验证规则
  if (newType === 'student') {
    loginRules.username[0].message = '请输入学号'
  } else {
    loginRules.username[0].message = '请输入工号'
  }
})

// 刷新验证码
const refreshCaptcha = () => {
  // 实际开发中应该调用后端接口刷新验证码
  loginForm.code = Math.floor(1000 + Math.random() * 9000).toString()
  ElMessage.success('验证码已发送: ' + loginForm.code)
}

// 登录处理
const handleLogin = () => {
  loginFormRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      
      // 模拟登录请求
      setTimeout(() => {
        loading.value = false
        
        if (loginForm.userType === 'student') {
          ElMessage.success('学生登录成功')
          router.push('/student/dashboard')
        } else {
          ElMessage.success('教师登录成功')
          router.push('/teacher/dashboard')
        }
      }, 1000)
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  
  /* 左侧区域样式 */
  .login-left {
    position: relative;
    width: 50%;
    height: 100%;
    color: white;
    background-image: url('https://img.zcool.cn/community/01a3865d4bde28a8012187f447cfff.jpg');
    background-size: cover;
    background-position: center;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .login-left-content {
      position: relative;
      z-index: 2;
      padding: 0 50px;
      text-align: center;
      
      .login-logo {
        margin-bottom: 60px;
        
        h1 {
          font-size: 42px;
          font-weight: 700;
          margin: 0;
          letter-spacing: 2px;
          text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }
        
        p {
          font-size: 18px;
          margin: 10px 0 0;
          opacity: 0.8;
          letter-spacing: 1px;
        }
      }
      
      .login-slogan {
        h2 {
          font-size: 28px;
          font-weight: 600;
          margin: 0 0 15px;
          text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }
        
        p {
          font-size: 16px;
          opacity: 0.8;
          margin: 0;
        }
      }
    }
    
    .login-bg-overlay {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(135deg, rgba(25, 118, 210, 0.8) 0%, rgba(100, 65, 165, 0.9) 100%);
      z-index: 1;
    }
  }
  
  /* 右侧区域样式 */
  .login-right {
    width: 50%;
    height: 100%;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
    
    .login-form-container {
      width: 100%;
      max-width: 450px;
      
      .login-header {
        text-align: center;
        margin-bottom: 40px;
        
        h2 {
          font-size: 28px;
          color: #303133;
          margin: 0;
        }
        
        p {
          font-size: 16px;
          color: #909399;
          margin: 15px 0 0;
        }
      }
      
      .login-form {
        .el-form-item:first-child {
          text-align: center;
          margin-bottom: 30px;
        }
        
        .custom-input {
          .el-input__wrapper {
            padding: 0 15px;
            height: 50px;
            box-shadow: 0 0 0 1px #dcdfe6 inset;
            border-radius: 6px;
            transition: all 0.3s;
            
            &:hover {
              box-shadow: 0 0 0 1px #c0c4cc inset;
            }
            
            &.is-focus {
              box-shadow: 0 0 0 1px #409eff inset;
            }
          }
          
          .el-input__prefix-inner {
            display: flex;
            align-items: center;
            color: #909399;
            font-size: 18px;
            margin-right: 10px;
          }
        }
        
        .code-input {
          display: flex;
          gap: 15px;
          
          .el-input {
            flex: 1;
          }
          
          .captcha-btn {
            width: 120px;
            font-size: 14px;
            border-radius: 6px;
          }
        }
        
        .remember-forgot {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
        
        .login-button {
          width: 100%;
          margin-top: 20px;
          height: 50px;
          font-size: 16px;
          border-radius: 6px;
          background: linear-gradient(135deg, #1976d2 0%, #6441a5 100%);
          border: none;
          letter-spacing: 1px;
          
          &:hover {
            background: linear-gradient(135deg, #1565c0 0%, #5e35b1 100%);
          }
        }
      }
      
      .login-footer {
        text-align: center;
        margin-top: 40px;
        
        p {
          color: #909399;
          font-size: 14px;
        }
      }
    }
  }
}

/* 响应式设计 */
@media screen and (max-width: 992px) {
  .login-container {
    flex-direction: column;
    
    .login-left,
    .login-right {
      width: 100%;
    }
    
    .login-left {
      height: 40%;
      
      .login-left-content {
        .login-logo {
          margin-bottom: 20px;
          
          h1 {
            font-size: 32px;
          }
          
          p {
            font-size: 16px;
          }
        }
        
        .login-slogan {
          h2 {
            font-size: 22px;
          }
          
          p {
            font-size: 14px;
          }
        }
      }
    }
    
    .login-right {
      height: 60%;
      padding: 20px;
      
      .login-form-container {
        max-width: 380px;
        
        .login-header {
          margin-bottom: 20px;
          
          h2 {
            font-size: 24px;
          }
          
          p {
            font-size: 14px;
            margin-top: 10px;
          }
        }
      }
    }
  }
}
</style> 